package br.indt.framework.ui.common;

import com.nokia.mid.ui.frameanimator.FrameAnimator;
import com.nokia.mid.ui.gestures.GestureEvent;
import com.nokia.mid.ui.gestures.GestureInteractiveZone;
import com.nokia.mid.ui.gestures.GestureListener;
import com.nokia.mid.ui.gestures.GestureRegistrationManager;

public class CirclesGestureListener implements GestureListener {
	private Display display = null;
	private GestureInteractiveZone zone = null;
	private GestureInteractiveZone zone2 = null;

	private GestureInteractiveZone zone3 = null;

	public static final int BUTTON_BACK_SIZE = 46;

	public CirclesGestureListener(Display display) {
		int menubar_size = 0;

//		if (UIManager.getDeviceType() == UIManager.NEXT_GENERATION) {
//			menubar_size = Display.MENU_BAR_HEIGHT_NG;
//		}else if (UIManager.getDeviceType() != UIManager.FULL_TOUCH) {
//			menubar_size = UIManager.isTouchAndType()?Display.MENU_BAR_HEIGHT_TOUCH:Display.MENU_BAR_HEIGHT;
//		}

		this.display = display;
		GestureRegistrationManager.setListener(display, this);

		//widgets zone
		zone = new GestureInteractiveZone(GestureInteractiveZone.GESTURE_ALL);
//		if (UIManager.getDeviceType() == UIManager.NEXT_GENERATION) {
//			zone.setRectangle(0, Display.TITLE_BAR_HEIGHT, display.getWidth(), display.getHeight() - menubar_size);
//		}else{
			zone.setRectangle(0, Display.TITLE_BAR_HEIGHT, display.getWidth(), display.getHeight() - (Display.TITLE_BAR_HEIGHT + menubar_size));
//		}
		GestureRegistrationManager.register(display, zone);

		//menuBar zone
		zone2 = new GestureInteractiveZone(GestureInteractiveZone.GESTURE_TAP);

		if (isFullTouch()) {
			zone2.setRectangle(display.getWidth() - BUTTON_BACK_SIZE, display.getHeight() - BUTTON_BACK_SIZE, display.getWidth(), display.getHeight());

		} else {
			zone2.setRectangle(0, display.getHeight() - menubar_size, display.getWidth(), display.getHeight());
		}
		GestureRegistrationManager.register(display, zone2);

		//TitleBar Zone
		zone3 = new GestureInteractiveZone(GestureInteractiveZone.GESTURE_TAP);
		zone3.setRectangle(0, 0, display.getWidth(), Display.TITLE_BAR_HEIGHT);
		GestureRegistrationManager.register(display, zone3);
	}

	public void gestureAction(Object container, GestureInteractiveZone gestureInteractiveZone, GestureEvent gestureEvent) {
		Display tc = (Display) container;
		FrameAnimator fa = (FrameAnimator) tc.getFrameAnimator();
		switch (gestureEvent.getType()) {
		case GestureInteractiveZone.GESTURE_FLICK:
		{
			if (!display.getCurrentDisplayable().isPopUp()) {
				fa.kineticScroll(gestureEvent.getFlickSpeed(),
	                    FrameAnimator.FRAME_ANIMATOR_FREE_ANGLE,
	                    FrameAnimator.FRAME_ANIMATOR_FRICTION_LOW,
	                    gestureEvent.getFlickDirection());
			}
			break;
		}

		case GestureInteractiveZone.GESTURE_TAP:
		{
			if(gestureInteractiveZone == zone) {
				
				if(display != null) {
						switch (Display.getDeviceType()) {
						case Display.TOUCH_TYPE:{
							display.getCurrentDisplayable().click(gestureEvent.getStartX(), gestureEvent.getStartY());
						break;
						}
						
						case Display.FULL_TOUCH:{
							  if (display.getLoadingDisplayable() == null && !display.getCurrentDisplayable().getName().equals("OptionMenuFullTouch")){
								  if (!isBackButton(gestureEvent)) {
									  display.getCurrentDisplayable().click(gestureEvent.getStartX(), gestureEvent.getStartY());
								  }
							  }else if (display.getLoadingDisplayable() != null  &&(!(isBackButton(gestureEvent)))){
								  display.getLoadingDisplayable().click(gestureEvent.getStartX(), gestureEvent.getStartY());
							  }else if(display.getCurrentDisplayable().getName().equals("OptionMenuFullTouch")&&(!(isBackButton(gestureEvent)))){
								  if (!display.getScreenListener().screenClicked(gestureEvent.getStartX(), gestureEvent.getStartY())) {
									display.getCurrentDisplayable().click(gestureEvent.getStartX(), gestureEvent.getStartY());
								  }
							  }
							  
							
							  break;
						}
						case Display.NEXT_GENERATION:{
							display.getCurrentDisplayable().click(gestureEvent.getStartX(), gestureEvent.getStartY());
						break;
						}	
					}
			
				}


			} else if(gestureInteractiveZone == zone2 && !display.getCurrentDisplayable().getName().equals("OptionMenuFullTouch")) {
					
					Displayable result = display.getCurrentDisplayable();
					if(display != null && result != null) {
						if(result.getMenuBar() != null) {
							if(isFullTouch()){
								if(display.getLoadingDisplayable() == null){
									result.menuClickEvent(gestureEvent.getStartX(), gestureEvent.getStartY());
								}
								
							}else{
								result.menuClickEvent(gestureEvent.getStartX(), gestureEvent.getStartY());	
							}
							
						} else {
							result.click(gestureEvent.getStartX(), gestureEvent.getStartY());
						}
					}
				

			} else if(gestureInteractiveZone == zone3  && !display.getCurrentDisplayable().getName().equals("OptionMenuFullTouch")) {

				if(display != null && display.getCurrentDisplayable() != null) {
					if(display.getCurrentDisplayable().getTitleBar() != null) {
						display.getCurrentDisplayable().releaseTextEditor();
						display.getCurrentDisplayable().titleClickEvent(gestureEvent.getStartX(), gestureEvent.getStartY());

					} else {
						display.getCurrentDisplayable().click(gestureEvent.getStartX(), gestureEvent.getStartY());
					}
				} 
			}
			break;
		}

		case GestureInteractiveZone.GESTURE_LONG_PRESS:
		{
			if(gestureInteractiveZone == zone) {
				if(display != null && display.getCurrentDisplayable() != null) {
					display.getCurrentDisplayable().longPressed(gestureEvent.getStartX(), gestureEvent.getStartY());
				}

			} else if(gestureInteractiveZone == zone2) {
				if(display != null && display.getCurrentDisplayable() != null) {
					if(display.getCurrentDisplayable().getMenuBar() == null) {
						display.getCurrentDisplayable().longPressed(gestureEvent.getStartX(), gestureEvent.getStartY());
					}
				} 
			}
			break;
		}

		case GestureInteractiveZone.GESTURE_DRAG:
		{
			try {
				if (!display.getCurrentDisplayable().isPopUp()) {
					if(display != null && display.getCurrentDisplayable() != null) {
						display.getCurrentDisplayable().drag(gestureEvent.getStartX(), gestureEvent.getStartY());
					}
	
					tc.getCurrentDisplayable().setDragCounterY(tc.getCurrentDisplayable().getDragCounterY() + gestureEvent.getDragDistanceY());
					int newY = tc.getCurrentDisplayable().getCurrentY() + tc.getCurrentDisplayable().getDragCounterY();
		            fa.drag(0, newY);
				}
			} catch (Exception e) {
				//Utils.logError("GESTURE_DRAG - " + e.getMessage());
			}
			break;
		}

		case GestureInteractiveZone.GESTURE_DROP:
			break;

		default:
			break;
		}
	}

	private boolean isNG() {
		return Display.getDeviceType() == Display.NEXT_GENERATION;
	}

	private boolean isFullTouch() {
		return Display.getDeviceType() == Display.FULL_TOUCH;
	}

	private boolean isBackButton(GestureEvent gestureEvent) {
		if (gestureEvent.getStartX() >= (display.getWidth() - BUTTON_BACK_SIZE) && gestureEvent.getStartY()>=(display.getHeight() - BUTTON_BACK_SIZE)){
			return true;
		}
		return false;
	}
}
